//
//  JSMStrategyDetailViewController.m
//  TakeYouTourTogether
//
//  Created by king on 15/12/5.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMStrategyDetailViewController.h"
#import "JSMStrategyContinentCollectionViewCell.h"
#import "JSMStrategyContinentTableViewCell.h"
#import "JSMStrategyElementsModel.h"
#import "JSMStrategyModel.h"
#import "JSMStrategyContinentDetailViewController.h"

@interface JSMStrategyDetailViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDataSource, UITableViewDelegate, JSMStrategyContinentCollectionViewCellDelegate>

@property(nonatomic, strong) BaseCollectionView *myCollection;
@property(nonatomic, strong) BaseTableView *myTableView;
@property(nonatomic, strong) UIView *errorView;
@property(nonatomic, strong) NSArray *array;
@property(nonatomic, strong) NSArray *dataArray;
// 五大洲数组
@property(nonatomic, strong) NSArray *dataTrendArray;
@property(nonatomic, strong) NSArray *dataEuropeArray;
@property(nonatomic, strong) NSArray *dataHKMSARTaiwanArray;
@property(nonatomic, strong) NSArray *dataAmericaArray;
@property(nonatomic, strong) NSArray *dataAsiaAarray;
@property(nonatomic, strong) NSArray *dataDomesticArray;

@end

@implementation JSMStrategyDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = COLOR(225, 214, 184, 1.0);
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self requestDataFromNetWorking];
    
}
#pragma mark - Method createCollectionViewForeignTour
- (BaseCollectionView *)myCollection {
    if (!_myCollection) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(80, 120);
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        
        self.myCollection = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];
        [self.view addSubview:self.myCollection];
        [self.myCollection mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_top);
            make.width.mas_equalTo(80);
            make.height.equalTo(self.view.mas_height);
        }];
        self.myCollection.backgroundColor = [UIColor clearColor];
        self.myCollection.showsHorizontalScrollIndicator = NO;
        self.myCollection.showsVerticalScrollIndicator = NO;
        self.myCollection.delegate = self;
        self.myCollection.dataSource = self;
        [self.myCollection registerClass:[JSMStrategyContinentCollectionViewCell class] forCellWithReuseIdentifier:@"ad"];
        self.array = @[@"热门地点", @"欧洲", @"港澳台", @"美洲", @"亚洲"];
        
        
    }
    return _myCollection;
}
#pragma mark - Method createCollectionDetailViewForeignTour
- (BaseTableView *)myTableView {
    if (!_myTableView) {
        self.myTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(80, 0, SCREEN_WIDTH-80, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
        self.myTableView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:self.myTableView];
        self.myTableView.delegate = self;
        self.myTableView.dataSource = self;
    }
    return _myTableView;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JSMStrategyContinentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ad" forIndexPath:indexPath];
    cell.delegate = self;
    cell.str = self.array[indexPath.row];
    cell.number = indexPath.item;
    cell.nownumber = self.path.item;
//    if (i == 5) {
//        UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:self.path];
//        JSMStrategyContinentCollectionViewCell *touchcell = (JSMStrategyContinentCollectionViewCell *)cell;
//        [touchcell touchcell];
//    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击collectionviewCell");
    switch (indexPath.row) {
        case ContinentAllMessageTypeTrend: {
            self.dataArray = self.dataTrendArray;
            if (self.dataTrendArray == 0) {
                [self.myTableView removeFromSuperview];
                self.type = ContinentAllMessageTypeTrend;
                [self requestDataFromNetWorking];
            }
        }
            break;
        case ContinentAllMessageTypeEurope: {
            self.dataArray = self.dataEuropeArray;
            if (self.dataEuropeArray == 0) {
                [self.myTableView removeFromSuperview];
                self.type = ContinentAllMessageTypeEurope;
                [self requestDataFromNetWorking];
            }
            
        }
            break;
        case ContinentAllMessageTypeHKMSARTaiwan: {
            self.dataArray = self.dataHKMSARTaiwanArray;
            if (self.dataHKMSARTaiwanArray == 0) {
                [self.myTableView removeFromSuperview];
                self.type = ContinentAllMessageTypeHKMSARTaiwan;
                [self requestDataFromNetWorking];
            }
        }
            break;
        case ContinentAllMessageTypeAmerica: {
            self.dataArray = self.dataAmericaArray;
            if (self.dataAmericaArray == 0) {
                [self.myTableView removeFromSuperview];
                self.type = ContinentAllMessageTypeAmerica;
                [self requestDataFromNetWorking];
            }
        }
            break;
        case ContinentAllMessageTypeAsia: {
            self.dataArray = self.dataAsiaAarray;
            if (self.dataAsiaAarray == 0) {
                [self.myTableView removeFromSuperview];
                self.type = ContinentAllMessageTypeAsia;
                [self requestDataFromNetWorking];
            }
            
        }
            break;
        case ContinentAllMessageTypeDomestic: {
            self.dataArray = self.dataDomesticArray;
            if (self.dataDomesticArray == 0) {
                [self.myTableView removeFromSuperview];
                self.type = ContinentAllMessageTypeDomestic;
                [self requestDataFromNetWorking];
            }
            
        }
            break;
    }
    if (self.dataArray == 0) {
        [self.myTableView removeFromSuperview];
    } else {
        [self.view addSubview:self.myTableView];
    }
    if (self.path != indexPath) {
        
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    JSMStrategyContinentCollectionViewCell *touchcell = (JSMStrategyContinentCollectionViewCell *)cell;
    [touchcell touchcell];
    UICollectionViewCell *upcell = [collectionView cellForItemAtIndexPath:self.path];
    JSMStrategyContinentCollectionViewCell *uptouchcell = (JSMStrategyContinentCollectionViewCell *)upcell;
    [uptouchcell uptouchcell];
    self.path = indexPath;
    [self.myTableView reloadData];
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 240*(SCREEN_WIDTH-80)/380;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *resue = @"strategyContinentTableViewResue";
    JSMStrategyContinentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:resue];
    if (!cell) {
        cell = [[JSMStrategyContinentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:resue];
    }
//    JSMStrategyElementsModel *elementTemp = self.elementsArray[2];
    JSMStrategyModel *temp = self.dataArray[indexPath.row];
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:temp.cover_route_map_cover] placeholderImage:[UIImage imageNamed:PLACE_HOLDER_IMAGE]];
    cell.myLabel.text = temp.name;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击tableViewCell");
    JSMStrategyContinentDetailViewController *controller = [[JSMStrategyContinentDetailViewController alloc] init];
    controller.tempModel = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:controller animated:YES];
}
#pragma mark - Method requestDataFromNetWorking 网络请求数据
- (void)requestDataFromNetWorking {
    [AFNetworkingTool getUrl:[JSMContinentAllMessageType getUrlForContinentAllMessageType:self.type] hudSuperView:self.view result:JSON success:^(id result) {
        NSDictionary *dic = result;
        NSLog(@"%@", dic);//elements data
        self.dataArray = [JSMStrategyModel baseModelByArr:dic[@"data"]];
        switch (self.type) {
            case ContinentAllMessageTypeTrend: {
                self.dataTrendArray = self.dataArray;
            }
                break;
            case ContinentAllMessageTypeEurope: {
                self.dataEuropeArray = self.dataArray;
            }
                break;
            case ContinentAllMessageTypeHKMSARTaiwan: {
                self.dataHKMSARTaiwanArray = self.dataArray;
            }
                break;
                
            case ContinentAllMessageTypeAmerica: {
                self.dataAmericaArray = self.dataArray;
            }
                break;
            case ContinentAllMessageTypeAsia: {
                self.dataAsiaAarray = self.dataArray;
            }
                break;
            case ContinentAllMessageTypeDomestic: {
                self.dataDomesticArray = self.dataArray;
            }
                break;
        }
//        [self createCollectionViewForeignTour];
//        [self createCollectionDetailViewForeignTour];
        [self.view addSubview:self.myCollection];
        [self.view addSubview:self.myTableView];
        // tableView 刷新
        [self.myTableView reloadData];
        
    } failure:^(NSError *error){
        self.errorView = [[UIView alloc] init];
        self.errorView.backgroundColor = [UIColor clearColor];
        self.errorView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        UIImageView *myImageView = [[UIImageView alloc] init];
        myImageView.frame = CGRectMake(50, 50, SCREEN_WIDTH-100, SCREEN_HEIGHT/2-100);
        myImageView.image = [UIImage imageNamed:@"iconfont-no-wifi-2.png"];
        [self.errorView addSubview:myImageView];

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"重新加载" forState:UIControlStateNormal];
        button.frame = CGRectMake(SCREEN_WIDTH/2-50, SCREEN_HEIGHT/2+20, 100, 40);
        [button setBackgroundColor:[UIColor blueColor]];
        [button addTarget:self action:@selector(refreshingData:) forControlEvents:UIControlEventTouchUpInside];
        [self.errorView addSubview:button];
        [self.view addSubview:self.errorView];
    }];
}
#pragma mark - Method 刷新按钮
- (void)refreshingData:(UIButton *)button {
    [self requestDataFromNetWorking];
}
#pragma mark - Method didReceiveMemoryWarning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
